<?php
if(!$Auth->checkAuth()) // if user is logged in
{
    return;
}

$day = date("d"); //current day 
$month = date("m"); //current month
$year = date("Y"); // current year
$sql1 = mysql_query("SELECT balance FROM users WHERE uid = ".$Loggeduser->uid." LIMIT 1");
if(!mysql_num_rows($sql1))
{
    die("Invalid User"); // Record not found = Invalid User
}

$stats = mysql_fetch_object($sql1);
$today_earnings = 0.00;

$total_earnings = sprintf("%.2f",$stats->balance);
$refCount = getReferralCount($Auth->getLoggedId()); //user total referrals



//Today Earning
$sql1 = mysql_query("select SUM(credits) as credits FROM earnings_log where day(`date`) = '$day' AND month(`date`) = '$month' AND year(`date`) = '$year' AND uid = '".$Loggeduser->uid."'"); //today earning sql
if(mysql_num_rows($sql1))
{
    $row = mysql_fetch_object($sql1);
    $today_earnings =  $row->credits;

	
		
}
$today_earnings = sprintf("%.2f",	$today_earnings);



//Today Downloads
$tdsql = mysql_query("SELECT COUNT(d.id) as total FROM downloads_log as d RIGHT JOIN files as f ON d.file_uniqid = f.code WHERE f.uid = '".$Loggeduser->uid."' AND DATE(d.date) = CURDATE()");
if(mysql_num_rows($tdsql))
{
	$ttm = mysql_fetch_object($tdsql);
	$today_downloads = $ttm->total;
}else
{
    $today_downloads = 0;
}



//Today Downloads Earning
$dsql1 = mysql_query("select SUM(credits) as credits, COUNT(id) as numOfT FROM earnings_log where (notes LIKE '%Offer%' OR notes LIKE '%Gateway Lead%' ) AND DATE(date) = CURDATE() AND file_id != 0 AND uid = '".$Loggeduser->uid."'"); 
if(mysql_num_rows($dsql1))
{
    $drow = mysql_fetch_object($dsql1);
    $today_downloads_earnings =  $drow->credits;
	
	$numOfT = $drow->numOfT;
	$avgcpa = sprintf("%.2f", $today_downloads_earnings / $numOfT);

	
		
}else
{
    $avgcpa = '0.00';	
}
$today_downloads_earnings = sprintf("%.2f",	$today_downloads_earnings);



//Yesterday Downloads Earning
$ydsql1 = mysql_query("select SUM(credits) as credits FROM earnings_log where (notes LIKE '%Offer%' OR notes LIKE '%Gateway Lead%' ) AND DATE(date) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND  uid = '".$Loggeduser->uid."'"); 
if(mysql_num_rows($ydsql1))
{
    $ydrow = mysql_fetch_object($ydsql1);
    $yesterday_downloads_earnings =  $ydrow->credits;
	
		
}else
{
    $yesterday_downloads_earnings = '0.00';	
}
$yesterday_downloads_earnings = sprintf("%.2f",	$yesterday_downloads_earnings);




//All Time Downloads Earning
$adsql1 = mysql_query("select SUM(credits) as credits FROM earnings_log where (notes LIKE '%Offer%' OR notes LIKE '%Gateway Lead%' )  AND file_id != 0 AND uid = '".$Loggeduser->uid."'"); 
if(mysql_num_rows($adsql1))
{
    $adrow = mysql_fetch_object($adsql1);
    $all_downloads_earnings =  $adrow->credits;

	
		
}else
{
    $all_downloads_earnings = '0.00';	
}
$all_downloads_earnings = sprintf("%.2f",	$all_downloads_earnings);
$template->assign('all_downloads_earnings', $all_downloads_earnings);



//Today Referrals Earning
$rsql1 = mysql_query("select SUM(credits) as credits FROM earnings_log where notes LIKE '%Referral%' AND day(`date`) = '$day' AND month(`date`) = '$month' AND year(`date`) = '$year' AND file_id = 0 AND uid = '".$Loggeduser->uid."'"); 
if(mysql_num_rows($rsql1))
{
    $rrow = mysql_fetch_object($rsql1);
    $today_referrals_earnings =  $rrow->credits;
		
}
$today_referrals_earnings = sprintf("%.2f",	$today_referrals_earnings);


////Yesterday Referrals Earning
$yrsql2 = mysql_query("select SUM(credits) as credits FROM earnings_log where notes LIKE '%Referral%' AND DATE(date) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))  AND uid = '".$Loggeduser->uid."'"); 
if(mysql_num_rows($yrsql2))
{
    $yrrow2 = mysql_fetch_object($yrsql2);
    $yesterday_referrals_earnings =  $yrrow2->credits;
		
}
$yesterday_referrals_earnings = sprintf("%.2f",	$yesterday_referrals_earnings);



////AllTime Referrals Earning
$arsql2 = mysql_query("select SUM(credits) as credits FROM earnings_log where notes LIKE '%Referral%' AND file_id = 0 AND uid = '".$Loggeduser->uid."'"); 
if(mysql_num_rows($arsql2))
{
    $arrow2 = mysql_fetch_object($arsql2);
    $all_referrals_earnings =  $arrow2->credits;
		
}
$all_referrals_earnings = sprintf("%.2f",	$all_referrals_earnings);
$template->assign('all_referrals_earnings', $all_referrals_earnings); 




//Month Premium Earning
$psql1 = mysql_query("select SUM(credits) as credits FROM earnings_log where day(`date`) = '$day' AND month(`date`) = '$month' AND year(`date`) = '$year' AND file_id != 0 AND uid = '".$Loggeduser->uid."' AND notes LIKE '%Premium%' AND src_uid != 0"); 
if(mysql_num_rows($psql1))
{
    $prow1 = mysql_fetch_object($psql1);
    $today_premium_earnings =  $prow1->credits;
		
}

$today_premium_earnings = sprintf("%.2f",	$today_premium_earnings);



//Assign stats variables to template

$template->assign('today_downloads', $today_downloads); 
$template->assign('today_earnings', $today_earnings); 
$template->assign('avgcpa', $avgcpa); 


$template->assign('today_premium_earnings', $today_premium_earnings); 
$template->assign('today_downloads_earnings', $today_downloads_earnings); 
$template->assign('today_referrals_earnings', $today_referrals_earnings); 


$template->assign('yesterday_downloads_earnings', $yesterday_downloads_earnings); 
$template->assign('yesterday_referrals_earnings', $yesterday_referrals_earnings); 



//Month Earning
$sql2 = mysql_query("select SUM(credits) as credits FROM earnings_log where  month(`date`) = '$month' AND year(`date`) = '$year' AND uid = '".$Loggeduser->uid."'"); //today earning sql
if(mysql_num_rows($sql2))
{
    $row2 = mysql_fetch_object($sql2);
    $month_earnings =  $row2->credits;
		
}
$month_earnings = sprintf("%.2f",	$month_earnings);


//Month Downloads Earning
$dsql2 = mysql_query("select SUM(credits) as credits FROM earnings_log where  month(`date`) = '$month' AND year(`date`) = '$year' AND file_id != 0 AND notes LIKE '%Offer%' AND uid = '".$Loggeduser->uid."'"); 
if(mysql_num_rows($dsql2))
{
    $drow2 = mysql_fetch_object($dsql2);
    $month_downloads_earnings =  $drow2->credits;
		
}
$month_downloads_earnings = sprintf("%.2f",	$month_downloads_earnings);




//Month Referrals Earning
$rsql2 = mysql_query("select SUM(credits) as credits FROM earnings_log where  notes LIKE '%Referral%' AND  month(`date`) = '$month' AND year(`date`) = '$year' AND file_id = 0 AND uid = '".$Loggeduser->uid."'"); 
if(mysql_num_rows($rsql2))
{
    $rrow2 = mysql_fetch_object($rsql2);
    $month_referrals_earnings =  $rrow2->credits;
		
}
$month_referrals_earnings = sprintf("%.2f",	$month_referrals_earnings);


//Month Premium Earning
$psql2 = mysql_query("select SUM(credits) as credits FROM earnings_log where  month(`date`) = '$month' AND year(`date`) = '$year' AND file_id != 0 AND uid = '".$Loggeduser->uid."' AND notes LIKE '%Premium%' AND src_uid != 0"); 
if(mysql_num_rows($psql2))
{
    $prow2 = mysql_fetch_object($psql2);
    $month_premium_earnings =  $prow2->credits;
		
}

$month_premium_earnings = sprintf("%.2f",	$month_premium_earnings);


//Assign stats variables to template
$template->assign('month_earnings', $month_earnings); 
$template->assign('month_premium_earnings', $month_premium_earnings); 
$template->assign('month_downloads_earnings', $month_downloads_earnings); 
$template->assign('month_referrals_earnings', $month_referrals_earnings); 



//Last Payment
$lpsql = mysql_query("SELECT amount FROM cashouts WHERE uid = '".$Loggeduser->uid."' AND status = 'Complete' ORDER BY id DESC LIMIT 1");
if(mysql_num_rows($lpsql))
{
    $lprow = mysql_fetch_object($lpsql);
    $last_payment =  $lprow->amount;
		
}else
{
    $last_payment =  '0';	
}




$template->assign('last_payment', $last_payment); 
$template->assign('direct_earnings', $direct_earnings); 
$template->assign('referral_earnings', $referral_earnings); 
$template->assign('total_earnings', $total_earnings); 
$template->assign('refCount', $refCount);

?>